package com.ctvit.cch.core.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctvit.cch.core.enums.AuditStatusEnum;
import com.ctvit.cch.core.mapper.CreativeTopicMapper;
import com.ctvit.cch.core.mapper.InspirationCreationMapper;
import com.ctvit.cch.core.mapper.InspirationRecordMapper;
import com.ctvit.cch.core.pojo.domain.CreativeTopic;
import com.ctvit.cch.core.pojo.domain.InspirationCreation;
import com.ctvit.cch.core.pojo.domain.InspirationRecord;
import com.ctvit.cch.core.pojo.dto.creative.CreativeTopicApprovedDTO;
import com.ctvit.cch.core.pojo.dto.creative.CreativeTopicEditDTO;
import com.ctvit.cch.core.pojo.dto.creative.CreativeTopicMyDTO;
import com.ctvit.cch.core.pojo.dto.creative.CreativeTopicPendingDTO;
import com.ctvit.cch.core.pojo.dto.creative.CreativeTopicReSubmitDTO;
import com.ctvit.cch.core.pojo.dto.creative.CreativeTopicSubmitDTO;
import com.ctvit.cch.core.pojo.vo.creative.CreativeTopicGetVO;
import com.ctvit.cch.core.pojo.vo.creative.CreativeTopicListVO;
import com.ctvit.cch.core.service.ICreativeTopicService;
import com.ruoyi.common.utils.SecurityUtils;

import cn.hutool.core.lang.Assert;

/**
 * 创意选题Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/10/11
 */
@Service
public class CreativeTopicServiceImpl extends ServiceImpl<CreativeTopicMapper, CreativeTopic>
        implements ICreativeTopicService {

    private final InspirationCreationMapper inspirationCreationMapper;

    private final InspirationRecordMapper inspirationRecordMapper;

    public CreativeTopicServiceImpl(InspirationCreationMapper inspirationCreationMapper,
            InspirationRecordMapper inspirationRecordMapper) {
        this.inspirationCreationMapper = inspirationCreationMapper;
        this.inspirationRecordMapper = inspirationRecordMapper;
    }

    @Override
    public List<CreativeTopicListVO> getMyList(CreativeTopicMyDTO dto) {
        // 查询我的创意选题列表
        return baseMapper.getMyList(dto);
    }

    @Override
    public List<CreativeTopicListVO> getPendingList(CreativeTopicPendingDTO dto) {
        // 查询待审批的创意选题列表
        return baseMapper.getPendingList(dto);
    }

    @Override
    public List<CreativeTopicListVO> getApprovedList(CreativeTopicApprovedDTO dto) {
        // 查询已审批的创意选题列表（已通过或未通过）
        return baseMapper.getApprovedList(dto);
    }

    @Override
    public boolean submit(CreativeTopicSubmitDTO dto) {
        InspirationRecord rone = inspirationRecordMapper.selectById(dto.getRecordId());
        Assert.notNull(rone, "灵感创作记录不存在");

        InspirationCreation cone = inspirationCreationMapper.selectById(rone.getCreationId());
        Assert.notNull(cone, "灵感创作不存在");
        Assert.isTrue(rone.getCreationId().equals(cone.getCreationId()), "灵感创作记录与创意选题不匹配");
        Assert.isTrue(cone.getCreateBy().equals(SecurityUtils.getUsername()), "无权限提交他人创建的创意选题");

        CreativeTopic topic = new CreativeTopic();
        topic.setTitle(rone.getTitle());
        topic.setContent(rone.getContent());
        topic.setDirection(cone.getDirection());
        topic.setSource(cone.getSource());

        topic.setSubmitterId(SecurityUtils.getUserId());
        topic.setAuditStatus(AuditStatusEnum.WAIT.getCode());

        return save(topic);
    }

    @Override
    public boolean reSubmit(CreativeTopicReSubmitDTO dto) {
        CreativeTopic one = getById(dto.getTopicId());
        Assert.notNull(one, "创意选题不存在");
        Assert.isTrue(one.getCreateBy().equals(SecurityUtils.getUsername()), "无权限提交他人创建的创意选题");

        // 使用 LambdaUpdateWrapper 更新选题状态为待审批
        return update(new LambdaUpdateWrapper<CreativeTopic>()
                .eq(CreativeTopic::getTopicId, dto.getTopicId())
                .set(CreativeTopic::getTitle, dto.getTitle())
                .set(CreativeTopic::getContent, dto.getContent())
                .set(CreativeTopic::getSubmitterId, SecurityUtils.getUserId())
                .set(CreativeTopic::getAuditStatus, AuditStatusEnum.WAIT.getCode()));
    }

    @Override
    public boolean withdraw(String id) {
        // 检查选题是否存在
        CreativeTopic one = getById(id);
        Assert.notNull(one, "创意选题不存在");
        Assert.isTrue(one.getCreateBy().equals(SecurityUtils.getUsername()), "无权限删除他人创建的选题");
        Assert.isTrue(AuditStatusEnum.WAIT.getCode().equals(one.getAuditStatus()), "只有待审批的选题才能撤回");

        // 更新选题状态为已撤回
        CreativeTopic topic = new CreativeTopic();
        topic.setTopicId(id);
        topic.setAuditStatus(AuditStatusEnum.WITHDRAWN.getCode());
        return updateById(topic);
    }

    @Override
    public CreativeTopicGetVO get(String id) {
        // 查询数据库获取创意选题详情
        CreativeTopic one = getById(id);
        Assert.notNull(one, "创意选题不存在");

        // 转换为 VO 对象
        CreativeTopicGetVO vo = new CreativeTopicGetVO();
        BeanUtils.copyProperties(Objects.requireNonNull(one), vo);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatchByIds(List<String> ids) {
        for (String id : ids) {
            // 检查选题是否存在
            CreativeTopic one = getById(id);
            Assert.notNull(one, "创意选题不存在");
            Assert.isTrue(one.getCreateBy().equals(SecurityUtils.getUsername()), "无权限删除他人创建的选题");

            // 删除创意选题
            removeById(id);
        }
        return true;
    }

    @Override
    public boolean edit(CreativeTopicEditDTO dto) {
        // 编辑创意选题
        String topicId = dto.getTopicId();
        CreativeTopic one = getById(topicId);
        Assert.notNull(one, "创意选题不存在");
        Assert.isTrue(one.getCreateBy().equals(SecurityUtils.getUsername()), "无权限编辑他人创建的选题");

        // 使用 LambdaUpdateWrapper 更新选题
        return update(new LambdaUpdateWrapper<CreativeTopic>()
                .eq(CreativeTopic::getTopicId, topicId)
                .set(CreativeTopic::getTitle, dto.getTitle())
                .set(CreativeTopic::getContent, dto.getContent()));

    }

}
